
//add a new child in current xml node;
function xmlAddNode(pNode,cName,cText){
	var xmlDoc = pNode.ownerDocument;
	var newNode = xmlDoc.createElement(cName);
	if(cText)newNode.text = cText;
	pNode.appendChild(newNode);
	return newNode;
}

function xmlAddCDATA(pNode,cText){
	var xmlDoc = pNode.ownerDocument;
	var newNode= xmlDoc.createCDATASection(cText);
	pNode.appendChild(newNode);
	return newNode;
}

function xmlMoveup(xnode){
	var prevNode = xnode.previousSibling;
	if(prevNode==null){
		return;
	}
	var pNode = xnode.parentNode;
	pNode.removeChild(xnode);
	pNode.insertBefore(xnode,prevNode);	
}

function xmlMovedown(xnode){
	var nextNode = xnode.nextSibling;
	if( nextNode==null ){
		return;
	}
	var pNode = xnode.parentNode;
		pNode.removeChild(xnode);
	
	var next2Node = nextNode.nextSibling;
	if( next2Node==null ){
		pNode.appendChild(xnode);
	}else{
		pNode.insertBefore(xnode,next2Node);	
	}
}

//remove all child nodes in current xml node;
function xmlClearNodes(pNode){
	var oNodeList = pNode.childNodes;
	for (var i=0; i<oNodeList.length; i++) {
	  pNode.removeChild(oNodeList.item(i));
	}
	return pNode;
}

//add a filed node in current xml node;
/*
 * type field type, if string will add ' ' auto in sql;
 * name field name;
 * link sql condition link key , like "=" "like" "is" "not like"....;
 * text the value for insert or compare;
 * label if select, in result set, it will show "select filed as label";
 */
function xmlAddDbField(pNode,type,name,link,text,label){
    var nodeField = xmlAddNode(pNode,"field",text);
     	nodeField.setAttribute("type",type);
    	nodeField.setAttribute("name",name);
    	nodeField.setAttribute("link",link?link:"=");
    	if(label)nodeField.setAttribute("label",label);
	return nodeField;
}

//add a conditions node in current xml node;
function xmlAddDbConditions(pNode,union){
    var conditions = xmlAddNode(pNode,"conditions");
     	conditions.setAttribute("union",union);
    if(union) conditions.setAttribute("union",union);
	return conditions;
}

//add a filed node in current xml node;
function xmlParseResponse(xmlDoc){
	var oReturn = {};
	oReturn.finished=false;
	oReturn.succeed	=false;
	oReturn.message	="";
	oReturn.errors	="";
	try{
		var root = oReturn.root = xmlDoc.documentElement;//.selectSingleNode("/root");
		var	xNode = root.selectSingleNode("response/finished");
		if( xNode ) oReturn.finished = (xNode.text.toLowerCase()=="true");
			xNode = root.selectSingleNode("response/succeed");
		if( xNode ) oReturn.succeed	 = (xNode.text.toLowerCase()=="true");
			xNode = root.selectSingleNode("response/message");
		if( xNode ) oReturn.message	 = xNode.text;
			xNode = root.selectSingleNode("response/errors");
		if( xNode ) oReturn.errors	 = xNode.text;
	}catch(e){}
	return oReturn;
}

function getXml() {// run in fireforx;


	//初始化对象
	var xmlDoc = document.implementation.createDocument("", "", null);	 
	xmlDoc.async="false";//取消xml在还没有完成数据完整填写就提交的动作
	
	//xml头部编写
	var header=xmlDoc.createProcessingInstruction("xml","version='1.0'"); 
	xmlDoc.appendChild(header);
	
	//创建子树
	var root = xmlDoc.createElement("loginInfo");
	var username = xmlDoc.createElement("userName");
	var userpassword = xmlDoc.createElement("passWord");

	//创建子树文本内容
	var usernameText=xmlDoc.createTextNode("nameValue");
	var passwordText=xmlDoc.createTextNode("pwdValue");
	
	
	//子树文本内容挂载
	username.appendChild(usernameText);
	userpassword.appendChild(passwordText);
	
	//子树节点挂载
	root.appendChild(username);
	root.appendChild(userpassword);
	
	//根内容挂载
	xmlDoc.appendChild(root);
	
	//返回xml文本内容
	return xmlDoc; 
};
